package com.attendcollatesys.util;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * excel 日期转换java date 的convert定义
 * add by gzl
 */
public class ExcelDateConvertUtil {


    public static class DateConverterY implements Converter<Date> {
        private static final String DATE_FORMAT = "yyyy-MM-dd";

        @Override
        public Class<?> supportJavaTypeKey() {
            return Date.class;
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        @Override
        public Date convertToJavaData(ReadConverterContext<?> context) throws Exception {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
            try {
                String stringValue = context.getReadCellData().getStringValue();
                if(StringUtils.isEmpty(stringValue)){
                    return null;
                }
                return dateFormat.parse(stringValue);
            } catch (ParseException e) {
                throw new Exception("日期转换异常 "+context.getReadCellData().getStringValue()+" 未转换成功");
            }

        }

    }

    public static class DateConverterYMT implements Converter<Date> {
        private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

        @Override
        public Class<?> supportJavaTypeKey() {
            return Date.class;
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        @Override
        public Date convertToJavaData(ReadConverterContext<?> context) throws Exception {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
            try {
                String stringValue = context.getReadCellData().getStringValue();
                if(StringUtils.isEmpty(stringValue)){
                    return null;
                }
                System.out.println(stringValue);
                return dateFormat.parse(stringValue);
            } catch (ParseException e) {
                throw new Exception("日期转换异常 "+context.getReadCellData().getStringValue()+" 未转换成功");
            }

        }

    }

    public static class DateConverterYM implements Converter<Date> {
        private static final String DATE_FORMAT = "yyyyMM";

        @Override
        public Class<?> supportJavaTypeKey() {
            return Date.class;
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        @Override
        public Date convertToJavaData(ReadConverterContext<?> context) throws Exception {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
            try {
                String stringValue = context.getReadCellData().getStringValue();
                if(StringUtils.isEmpty(stringValue)){
                    return null;
                }
                return dateFormat.parse(stringValue);
            } catch (ParseException e) {
                throw new Exception("日期转换异常 "+context.getReadCellData().getStringValue()+" 未转换成功");
            }

        }

    }
}
